<?php

class Default_CategoryController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
		$this->_auth = Zend_Auth::getInstance();
		$this->view->hasIdentity = $this->_auth->hasIdentity();
    }

    public function listAction()
    {
		$cate_model= new Application_Model_Category();
		
		if ($this->getRequest()->getParam('id','')) {
			$cate_id = $this->getRequest()->getParam('id');
			$cate = $cate_model->fetchAllOrderDesc(array("cat_id='{$cate_id}'")); 	
			$cate= (object)$cate[0];
		}else if($this->getRequest()->getParam('alias','')) {
			$cate_alias = $this->getRequest()->getParam('alias');
			$cate = $cate_model->fetchAllOrderDesc(array("cat_alias='{$cate_alias}'")); 
			$cate= (object)$cate[0];
			$cate_id = $cate->cat_id;
		}
		
		$post= new Application_Model_Post();
		
			
		$all_post = $post->getByCategory($cate_id,array('published=1'), 0);
		
		$paginator = Zend_Paginator::factory($all_post);
		$paginator->setItemCountPerPage(10);        
        $paginator->setPageRange(5);
        $currentPage = $this->_request->getParam('page',1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->data_page=$paginator;
        
		
		$this->view->data = $paginator;
		$this->view->cate = $cate;
		$this->view->headTitle('Danh mục ' .$cate->cat_title );
    }
	
	
	public function crawldataAction()
	{
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$page = isset($_GET['page']) ? $_GET['page']: 2;
		//$url = "http://www.omgubuntu.co.uk/category/gaming/page/";
		//$url = "http://www.omgubuntu.co.uk/category/themes-2/page/";
		//$url = "http://www.omgubuntu.co.uk/category/news/page/";
		$url="http://www.omgubuntu.co.uk/category/dev/page/";
		// echo 
		
		$html = file_get_contents($url . $page);
		preg_match_all('/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)www\.omgubuntu\.co\.uk\/\d{4}\/\d{2}\/[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i', $html, $result, PREG_PATTERN_ORDER);
		
		for($i=0; $i< count($result[0]); $i++) {
			$result[0][$i] = preg_replace('/\#[a-z\-\_]{0,}/', '',$result[0][$i]);
		}
		
		$data = array_unique ($result[0]);
		if ($data) {
			
			foreach ($data as $d) {
				$_insert = array(
					'url' => trim($d),
					'cate' => '3'
				);
				$dbAdapter->insert('get_post', $_insert);
				echo "Inserted $d <br />";
			}
			echo "Current page: $page.";
			echo '<meta http-equiv="refresh" content="1; url=http://localhost/zfirst/public/category/crawldata?page='.($page+1).'">';
		}
		else 
		{
			echo "END";
		}
	}
	
	public function crawldatapostAction()
	{
		require_once(APPLICATION_PATH . '/../library/My/Libs/simple_html_dom.php');
		$post = isset($_GET['post']) ? $_GET['post']: 1;
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		
		$result = $dbAdapter->fetchAll('SELECT * FROM get_post WHERE id = ?', $post);
		if(!$result)
			die("END");
		$result = $result[0];
		$html = file_get_html($result['url']);
		
		$a =array();
		foreach($html->find('.entry-title') as $element) 
		       $a['title'] = $element->plaintext ;
		       
  		foreach($html->find('.entry-content') as $element) 
		       $a['content'] = $element;
    
    	if ($a)
   		{
			$a['status'] = '1';
			$_insert = array(
				'created' => time(),
				'category_id' => $result['cate'],
				'title'=>$a['title'],
				'content' =>$a['content'],
			);
			$dbAdapter->insert('post', $_insert);
			echo "Inserted {$a['title']} <br />";
   			$dbAdapter->update('get_post', $a, 'id = '.$post);
   			echo "Updated $post ";
   			echo '<meta http-equiv="refresh" content="1; url=http://localhost/zfirst/public/category/crawldatapost?post='.($post+1).'">';
   		}
    	
		die();
	}
	
}

